package cn.iocoder.yudao.module.famous.doctor.controller.admin.archives;

import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.famous.doctor.controller.admin.archives.vo.*;
import cn.iocoder.yudao.module.famous.doctor.dal.dataobject.archives.ArchivesDO;
import cn.iocoder.yudao.module.famous.doctor.convert.archives.ArchivesConvert;
import cn.iocoder.yudao.module.famous.doctor.service.archives.ArchivesService;

@Tag(name = "管理后台 - 患者档案")
@RestController
@RequestMapping("/famous.doctor/archives")
@Validated
public class ArchivesController {

    @Resource
    private ArchivesService archivesService;

    @PostMapping("/create")
    @Operation(summary = "创建患者档案")
    @PreAuthorize("@ss.hasPermission('famous.doctor:archives:create')")
    public CommonResult<Long> createArchives(@Valid @RequestBody ArchivesCreateReqVO createReqVO) {
        return success(archivesService.createArchives(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新患者档案")
    @PreAuthorize("@ss.hasPermission('famous.doctor:archives:update')")
    public CommonResult<Boolean> updateArchives(@Valid @RequestBody ArchivesUpdateReqVO updateReqVO) {
        archivesService.updateArchives(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除患者档案")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('famous.doctor:archives:delete')")
    public CommonResult<Boolean> deleteArchives(@RequestParam("id") Long id) {
        archivesService.deleteArchives(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得患者档案")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('famous.doctor:archives:query')")
    public CommonResult<ArchivesRespVO> getArchives(@RequestParam("id") Long id) {
        ArchivesDO archives = archivesService.getArchives(id);
        return success(ArchivesConvert.INSTANCE.convert(archives));
    }

    @GetMapping("/list")
    @Operation(summary = "获得患者档案列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('famous.doctor:archives:query')")
    public CommonResult<List<ArchivesRespVO>> getArchivesList(@RequestParam("ids") Collection<Long> ids) {
        List<ArchivesDO> list = archivesService.getArchivesList(ids);
        return success(ArchivesConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得患者档案分页")
    @PreAuthorize("@ss.hasPermission('famous.doctor:archives:query')")
    public CommonResult<PageResult<ArchivesRespVO>> getArchivesPage(@Valid ArchivesPageReqVO pageVO) {
        PageResult<ArchivesDO> pageResult = archivesService.getArchivesPage(pageVO);
        return success(ArchivesConvert.INSTANCE.convertPage(pageResult));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出患者档案 Excel")
    @PreAuthorize("@ss.hasPermission('famous.doctor:archives:export')")
    @OperateLog(type = EXPORT)
    public void exportArchivesExcel(@Valid ArchivesExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ArchivesDO> list = archivesService.getArchivesList(exportReqVO);
        // 导出 Excel
        List<ArchivesExcelVO> datas = ArchivesConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "患者档案.xls", "数据", ArchivesExcelVO.class, datas);
    }

}
